<template>
  <div ref="imageWrapper">
    <!-- 查询区域 -->
    <div class="search_box">
      <a-form-model ref="form" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="searchQuery">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="" prop="">
              <a-tree-select
                v-model="superParam.modelId"
                show-line
                style="width: 100%"
                placeholder="请选择数据模型"
                :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                :tree-data="treeData"
                tree-default-expand-all
                @change="
                  (value, node, extra) => {
                    superParam.modelName = node[0]
                    getSearchAndTitleList(value)
                  }
                "
              >
              </a-tree-select>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="6">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
            </a-col> -->
        </a-row>
      </a-form-model>
    </div>
    <a-card :bordered="false">
      <div class="" style="margin-bottom: 16px">
        <span>自动预警</span>
      </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record" v-if="dataSource.length">
          <a @click="handleToYD(record)" style="color:red;">转疑点</a>
        </span>
      </a-table>
      <!-- <CheckOrganModal ref="departModal" @ok="updateQueryParam"></CheckOrganModal> -->
      <YDModel ref="YDModel" :routerData="$route.path" />
    </a-card>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import { postAction, getAction } from '@/api/manage'
import moment from 'moment'
// import detailModel from './searchResult/index'
// import CheckOrganModal from '../components/CheckOrganModal'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'

export default {
  name: 'ss',
  mixins: [JeecgListMixin, base64toImg],
  components: { YDModel },
  data() {
    return {
      // 表头
      queryParam: {
        accName: '',
        accSetId: ''
      },
      superParam: {
        modelId: undefined
      },
      rules: {
        accYear: [{ required: true, message: '请选择年度' }],
        accStartTime: [{ required: true, message: '请选择开始时间' }],
        accEndTime: [{ required: true, message: '请选择结束时间' }],
        accName: [{ required: true, message: '请选择账套' }]
      },
      columns: columns(this),
      yearPickShow: false,
      dataSource: [],
      treeData: [],
      loading: false,
      url: {
        selectTree: '/dataModel/queryDataModelTemplate',
        list: '/dataModel/findResultList',
        getSearchAndTitle: '/dataModel/queryTitleAndSearch'
      },
      disableMixinCreated: true
    }
  },
  computed: {},
  created() {
    this.loadTree()
  },
  methods: {
    handleToYD(record) {
      if (this.dataSource.length) {
        // const { startTime = '', endTime = '' } = this.queryParam
        let ydnr = undefined
        // if (record) {
        //   ydnr = `【科目代码：${record.subjectCode}】【科目名称：${record.subjectName}】`
        // }
        let time = `【${this.superParam.modelName}】`
        this.addYd(this.queryParam, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    loadTree() {
      var that = this
      that.treeData = []
      getAction(this.url.selectTree).then(res => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.treeData = this.getTree(res.result, '0')
          this.loading = false
        }
      })
    },
    getTree(treeData, pid) {
      let treeArr = []
      for (let i = 0; i < treeData.length; i++) {
        let node = treeData[i]
        if (node.pid == pid) {
          let newNode = {
            title: node.modelName,
            key: node.id,
            value: node.id,
            children: this.getTree(treeData, node.id)
          }
          treeArr.push(newNode)
        }
      }
      return treeArr
    },
    getSearchAndTitleList(id) {
      this.listData = []
      this.dataSource = []
      this.columns = columns(this)
      getAction(this.url.getSearchAndTitle, { id: id }).then(res => {
        this.loading = false
        if (res.success) {
          res.result.searchList.map((item, index) => {
            this.listData.push({
              queryType: item.searchType,
              queryKey: item.fieldEnName,
              fieldCnName: item.fieldCnName,
              queryValueStart: '',
              queryValueEnd: ''
            })

            this.getListData = _.cloneDeep(this.listData)
          })
          res.result.titleList.map((item, index) => {
            this.columns.push({
              title: item.fieldCnName,
              align: 'center',
              dataIndex: item.fieldEnName
            })
          })

          this.columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 100,
            scopedSlots: { customRender: 'action' }
          })

          this.loadData(1)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    loadData(arg) {
      debugger
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      console.log('columns', this.columns)
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.queryParam.listData = JSON.stringify(_.cloneDeep(this.listData))
      var params = this.getQueryParams() //查询条件
      this.loading = true
      postAction(this.url.list, params).then(res => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleDatechange(dates) {
      this.queryParam.startMonth = dates[0]
      this.queryParam.endMonth = dates[1]
    },
    updateQueryParam(data) {
      console.log(data, 'daat')
      let str1 = ''
      let str2 = ''
      data.map(item => {
        // TODO 账套～～～
        str1 += item.id + ','
        str2 += item.accName + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      this.queryParam.accSetId = str1
      this.queryParam.accName = str2
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then(res => {
          if (res.success) {
            this.$refs.table.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handleViewDetail(row) {
      this.$refs.detailModel.getPlanId(row)
      this.$refs.detailModel.visible = true
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
.search_box {
  max-width: 16vw;
  // overflow: auto;
  margin-top: 20px;
  height: 0;
  border-width: 0px 15px 38px 0px;
  border-style: none solid solid;
  border-color: transparent transparent orange;
  /deep/ .ant-select-selection {
    min-width: 15vw;
    color: #fff;
    font-size: 18px;
    border: none;
    box-shadow: none;
    background-color: transparent;
    .ant-select-arrow .ant-select-arrow-icon {
      color: #fff;
    }
    .ant-select-selection__placeholder,
    .ant-select-search__field__placeholder {
      color: #fff;
    }
  }
}
</style>